#include "all.h"

WaypointManager::WaypointManager()
{
	hasNewpoint = true;
	nearestwaypoint = MAX_DISTANCE;
	string fileName = "";
	char dir[128];
	_getcwd(dir, 128);
	fileName.append( dir );
	fileName.append( resultPath );
	fs.open( fileName );
	fs>>noskipws;
	bool ret = fs.is_open();
	if ( !ret )
	{
		//todo error
		return ;
	}
	
	fs.clear();
	
}

WaypointManager::~WaypointManager()
{
	fs.flush();
	fs.close();
}
/*
	struct waypoint
	{
	float3	position;
	int		icon_id;
	float3	direction;
	float	radius;
	}
*/
int WaypointManager::WriteAlltoFile()
{
	int recordlen = waypointset.size();
	for ( int i = 0 ; i < recordlen; i++ )
	{
		waypoint wp = waypointset[i] ;//TEST
		fs.seekg( i* RECORD_LEN + RECORD_LINE_INTERVAL * 0,ios::beg );
		fs<<waypointset[i].position.x ;
		fs.seekg( i* RECORD_LEN + RECORD_LINE_INTERVAL * 1 ,ios::beg) ;
		fs<<waypointset[i].position.y ;
		fs.seekg( i* RECORD_LEN + RECORD_LINE_INTERVAL * 2 ,ios::beg) ;
		fs<<waypointset[i].position.z ;
		fs.seekg( i* RECORD_LEN + RECORD_LINE_INTERVAL * 3 ,ios::beg) ;
		fs<<waypointset[i].icon_id ;
		fs.seekg( i* RECORD_LEN + RECORD_LINE_INTERVAL * 4 ,ios::beg) ;
		fs<<waypointset[i].direction.x ;
		fs.seekg( i* RECORD_LEN + RECORD_LINE_INTERVAL * 5 ,ios::beg) ;
		fs<<waypointset[i].direction.y ;
		fs.seekg( i* RECORD_LEN + RECORD_LINE_INTERVAL * 6 ,ios::beg) ;
		fs<<waypointset[i].direction.z ;
		fs.seekg( i* RECORD_LEN + RECORD_LINE_INTERVAL * 7 ,ios::beg) ;
		fs<<waypointset[i].radius << setw( 11) \
			<<"\n";
	}
	fs.flush();
	fs.clear();
	return 0;
}

int WaypointManager::SaveAWaypoint(vehicle2* veh)
{
	waypoint wp( veh );
	waypointset.push_back( wp );
	hasNewpoint = true ;
	
	return 0;
}

int WaypointManager::ReadAllFromFile()
{
	int rows = getRows();
	for ( int i = 0; i < rows ; i++ )
	{
		waypointdata wpd;
		fs.seekg( i * RECORD_LEN , ios::beg );
		string line = "";
		getline( fs, line );
		wpd.position.x = stringToFloat( line.substr( 0, 1*RECORD_LINE_INTERVAL ) );
		wpd.position.y = stringToFloat( line.substr( 1*RECORD_LINE_INTERVAL, 2*RECORD_LINE_INTERVAL ));
		wpd.position.z = stringToFloat( line.substr( 2*RECORD_LINE_INTERVAL, 3*RECORD_LINE_INTERVAL ) );
		wpd.icon_id = stringToInt( line.substr( 3*RECORD_LINE_INTERVAL, 4*RECORD_LINE_INTERVAL ));
		wpd.direction.x = stringToFloat( line.substr( 4*RECORD_LINE_INTERVAL, 5*RECORD_LINE_INTERVAL ) );
		wpd.direction.y = stringToFloat( line.substr( 5*RECORD_LINE_INTERVAL, 6*RECORD_LINE_INTERVAL ) );
		wpd.direction.z = stringToFloat( line.substr( 6*RECORD_LINE_INTERVAL, 7*RECORD_LINE_INTERVAL ) );
		wpd.radius = stringToInt( line.substr( 7*RECORD_LINE_INTERVAL, 8*RECORD_LINE_INTERVAL ) );
	}
	

	return 0;
}

int WaypointManager::GetDistance(vehicle2* veh)
{

	float distance = MAX_DISTANCE;
	for ( vector<waypoint>::iterator iter = waypointset.begin(); iter != waypointset.end(); ++iter )
	{
		float distanceTemp = iter->getdistance( veh );
		if ( distanceTemp < distance)
		{
			distance = distanceTemp;
		}
	}
	hasNewpoint = false;
	return distance;
}
string WaypointManager::GetDistanceStr(vehicle2* veh)
{
	return floatToString( GetDistance( veh ) );
}
int WaypointManager::stringToInt(string s)
{
	istringstream instr(s);
	int x ;
	instr>>x;
	return x;
}

float WaypointManager::stringToFloat(string s)
{
	istringstream instr(s);
	float x ;
	instr>>x;
	return x;
}
string WaypointManager::floatToString( float num )
{
	ostringstream outstr;
	outstr<<num;
	return outstr.str();
}
int WaypointManager::getRows()
{
	int rows = 0 ;
	fs.seekg( 0, ios::beg );
	char chr = '-';
	while ( fs>>chr )
	{
		switch( chr )
		{
		case '\n':
			rows++;
		default:
			break;
		}
	}
	fs.clear();
	return rows;
}
